﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace TreeGridDemo
{
    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
            _data = new ObservableCollection<People>
                        {
                            new People
                                {
                                    FirstName = "Jhon",
                                    LastName = "Doe",
                                    Age = 45,
                                    Childs = new ObservableCollection<People>
                                                 {
                                                     new People
                                                         {
                                                             FirstName = "Elisabeth",
                                                             LastName = "Doe",
                                                             Age = 22
                                                         }
                                                 }
                                },
                            new People
                                {
                                    FirstName = "Endy",
                                    LastName = "Domino",
                                    Age = 50,
                                    Childs = new ObservableCollection<People>
                                                 {
                                                     new People
                                                         {
                                                             FirstName = "Andy",
                                                             LastName = "Domino",
                                                             Age = 24
                                                         }
                                                 }
                                }
                        };
            treeGrid.ItemsSource = _data;
            treeGrid.SelectedIndex = 2;
            _column = (HierarchicalTextColumn) treeGrid.Columns[0];
            _column.Filter =
                data => (string.IsNullOrEmpty(_firstNameFilter) ||
                         ((People) data).FirstName.ToLower().Contains(_firstNameFilter.ToLower())) &&
                        (string.IsNullOrEmpty(_lastNameFilter) ||
                         ((People) data).LastName.ToLower().Contains(_lastNameFilter.ToLower()));
            _column.SortDirectionChanged +=
                (sender, e) =>
                    {
                        switch (_column.SortDirection)
                        {
                            case null:
                                ascButton.IsEnabled = true;
                                descButton.IsEnabled = true;
                                break;
                            case ListSortDirection.Ascending:
                                ascButton.IsEnabled = false;
                                descButton.IsEnabled = true;
                                break;
                            case ListSortDirection.Descending:
                                ascButton.IsEnabled = true;
                                descButton.IsEnabled = false;
                                break;
                        }
                    };
            treeGrid.SelectionChanged += (sender, e) =>addButton.IsEnabled = treeGrid.SelectedValue != null;
        }

        private readonly ObservableCollection<People> _data;

        private void AddClick(object sender, RoutedEventArgs e)
        {
            ((People) treeGrid.SelectedValue).Childs
                .Add(new People
                         {
                             FirstName = "Mery",
                             LastName = "Pupkins",
                             Age = 15
                         });
        }

        private string _firstNameFilter;

        private string _lastNameFilter;

        private readonly HierarchicalTextColumn _column;

        private void FirstNameFilterChanged(object sender, TextChangedEventArgs e)
        {
            _firstNameFilter = ((TextBox) sender).Text;
            treeGrid.Refresh();
        }

        private void LastNameFilterChanged(object sender, TextChangedEventArgs e)
        {
            _lastNameFilter = ((TextBox)sender).Text;
            treeGrid.Refresh();
        }

        private void AscClick(object sender, RoutedEventArgs e)
        {
            _column.SortDirection = ListSortDirection.Ascending;
        }

        private void DescClick(object sender, RoutedEventArgs e)
        {
            _column.SortDirection = ListSortDirection.Descending;
        }
    }
}
